package com.xuecheng.content.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.xuecheng.commons.constants.CourseConstants;
import com.xuecheng.commons.enums.ErrorCode;
import com.xuecheng.commons.model.dto.BindTeachplanMediaDto;
import com.xuecheng.commons.model.vo.MediaVo;
import com.xuecheng.commons.model.vo.ResponseResult;
import com.xuecheng.commons.utils.AuthInfoHolder;
import com.xuecheng.content.domain.CourseBase;
import com.xuecheng.content.domain.Teachplan;
import com.xuecheng.content.domain.TeachplanMedia;
import com.xuecheng.content.mapper.TeachplanMediaMapper;
import com.xuecheng.content.service.CourseBaseService;
import com.xuecheng.content.service.TeachplanMediaService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuecheng.content.service.TeachplanService;
import com.xuecheng.media.api.MediaClient;
import com.xuecheng.web.exception.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author itheima
 * @since 2022-07-30
 */
@Service
public class TeachplanMediaServiceImpl extends ServiceImpl<TeachplanMediaMapper, TeachplanMedia> implements TeachplanMediaService {


    @Autowired
    private TeachplanService teachplanService;

    @Autowired
    private CourseBaseService courseBaseService;

    @Autowired
    private MediaClient mediaClient;  // 下面有红线不影响

    // 媒资绑定
    @Override
    public ResponseResult association(BindTeachplanMediaDto dto) {
        // 1.校验参数合法性
        if (ObjectUtil.isEmpty(dto.getMediaId()) || ObjectUtil.isEmpty(dto.getTeachplanId())) {
            throw new BusinessException(ErrorCode.PARAMSERROR);
        }
        // 2.判断课程状态
        // 2-1 先查课程计划
        Teachplan teachplan = teachplanService.getById(dto.getTeachplanId());
        // 2-2 再查课程信息
        CourseBase courseBase = courseBaseService.getById(teachplan.getCourseId());
        // 2-3 判断（已提交、已审核、已发布，不允许绑定）
        String auditStatus = courseBase.getAuditStatus();
        if (StrUtil.equals(auditStatus, CourseConstants.AUDIT_COMMIT_STATUS) ||
                StrUtil.equals(auditStatus, CourseConstants.AUDIT_PASTED_STATUS) ||
                StrUtil.equals(auditStatus, CourseConstants.AUDIT_PUBLISHED_STATUS)) {
            throw new BusinessException(ErrorCode.UPDATEEERROR);
        }
        // 3.feign远程调用媒资微服务
        ResponseResult<MediaVo> responseResult = mediaClient.findById(dto.getMediaId());
        // 3-1 判断状态码
        if (responseResult.getCode()!=0) {
            throw new BusinessException(ErrorCode.UPDATEEERROR);
        }
        // 3-2 取出媒资对象
        MediaVo mediaVo = responseResult.getData();
        // 4.封装媒资绑定对象
        TeachplanMedia teachplanMedia = new TeachplanMedia();
        teachplanMedia.setMediaId(mediaVo.getId()); // 媒资id
        teachplanMedia.setTeachplanId(teachplan.getId()); // 计划id
        teachplanMedia.setCourseId(courseBase.getId()); // 课程id
        teachplanMedia.setMediaFilename(mediaVo.getFilename()); // 媒资名
        teachplanMedia.setCreateDate(DateUtil.date());// 创建时间
        teachplanMedia.setCreatePeople(AuthInfoHolder.getUserId().toString()); // 创建人
        // 5.保存
        this.save(teachplanMedia);
        // 6.返回ok
        return ResponseResult.okResult();
    }

    //媒资解绑
    @Override
    public ResponseResult deleteById(Long teachPlanMediaId, Long courseBaseId) {
        //1.根据id查询课程
        CourseBase courseBase = courseBaseService.getById(courseBaseId);
        //2.判断（已提交、已审核、已发布，不允许绑定）
        String auditStatus = courseBase.getAuditStatus();
        if (StrUtil.equals(auditStatus, CourseConstants.AUDIT_COMMIT_STATUS) ||
                StrUtil.equals(auditStatus, CourseConstants.AUDIT_PASTED_STATUS) ||
                StrUtil.equals(auditStatus, CourseConstants.AUDIT_PUBLISHED_STATUS)) {
            throw new BusinessException(ErrorCode.UPDATEEERROR);
        }
        //3.根据id删除
        this.removeById(teachPlanMediaId);
        //4.返回
        return ResponseResult.okResult();
    }
}
